<template>
  <div class='xtx-goods-page'>
    <div class="container" v-if="goodsData.categories">
      <!-- 面包屑 -->
      <GoodsBread :goodsData="goodsData"/>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!--左边-->
        <div class="media">
         <!-- 商品图片组件 -->
         <GoodsImage :images="goodsData.mainPictures" />
         <!-- 商品销售信息组件 -->
         <GoodsSales/>
        </div>
        <!--右边-->
        <div class="spec">
         <!-- 商品介绍组件 -->
         <GoodsName :goodsData="goodsData"/>
         <!-- sku组件  -->
         <GoodsSku :goodsData="goodsData" @change="skuChange" skuId="1369155859933827074" />
         <!-- 数量选择组件 -->
         <XtxNumbox label="数量" v-model="num" :min="1" :max="goodsData.inventory" />
         <!-- 按钮组件 -->
         <XtxButton @click="insertCart" style="margin-top:20px" type="primary" size="middle">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant :goodsId="goodsData.id"/>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs"><GoodsTabs/></div>
          <!-- 注意事项 -->
          <div class="goods-warn"><GoodWarn/></div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"/>
          <GoodsHot :type="2"/>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import GoodsBread from './components/goods-bread.vue'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsRelevant from './components/goods-relevant'
import GoodsTabs from './components/goods-tabs'
import GoodsHot from './components/goods-hot'
import GoodWarn from './components/goods-warn.vue'
import Message from '@/components/Message.js'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { provide, ref } from 'vue'
import { findGoods } from '../../api/goods'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsBread,
    GoodsImage,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsRelevant,
    GoodsTabs,
    GoodsHot,
    GoodWarn
  },
  setup () {
    const route = useRoute()
    const goodsData = ref({})
    const num = ref(1)
    findGoods(route.params.id).then(data => {
      console.log('findGoods', data)
      goodsData.value = data.result
    })

    const currSku = ref(null) // 用户选择的SKU
    const skuChange = (sku) => {
      console.log('父组件中，收到的sku是', sku)
      // 更新当前商品的价格
      if (sku.specsText) {
        goodsData.value.price = sku.price
        goodsData.value.oldPrice = sku.oldPrice
        goodsData.value.inventory = sku.inventory
        currSku.value = sku
      } else {
        currSku.value = null
      }
    }

    provide('goodsData', goodsData)
    const store = useStore()
    const insertCart = () => {
      if (!currSku.value) {
        Message({ type: 'error', text: '请选中规格' })
        return
      }
      if (num.value > goodsData.value.inventory) {
        Message({ type: 'error', text: '库存不足' })
        return
      }
      const cart = {
        id: goodsData.value.id,
        skuId: currSku.value.skuId,
        name: goodsData.value.name,
        picture: goodsData.value.mainPictures[0],
        price: currSku.value.oldPrice, // 老价格
        nowPrice: currSku.value.price, // 价格
        count: num.value, // 购买数量
        attrsText: currSku.value.specsText,
        selected: true, // 是否选中
        stock: currSku.value.inventory, // 商品的库存
        isEffective: true // 商品是否有效
      }
      console.log('要添加的商品是', cart)

      store.dispatch('cart/insertCart', cart).then(res => {
        Message({ type: 'success', text: '添加成功' })
      })
    }
    return { goodsData, num, skuChange, insertCart }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
